package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.i18n.I18Config;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.youbenzi.mdtool.tool.MDTool;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.File;
import java.net.URL;

import static com.github.aloxc.plugin.restplus.i18n.I18Config.ALLOW_LANGUAGE;

/**
 * 变量ui
 *
 * @author leero
 */
public class HelpForm1 extends JBPanel implements ISettingPanel {
    private JPanel rootPanel;
    private JTabbedPane helpTabbedPanel;
    private Dialog dialog;
    private Project project;

    public JPanel getRootPanel() {
        return rootPanel;
    }


    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    @Override
    public void setContext(SettingListenerContext context) {

    }

    public HelpForm1() {
        super();
        initComponent();
    }

    public HelpForm1(Project project, SettingListenerContext context) {
        super();
        this.project = project;
        setContext(context);
        initComponent();
    }


    private void initComponent() {
        initUi();
        initAction();
    }

    private void initUi() {
        GridLayoutManager rootLayoutManager = new GridLayoutManager(1, 1);
        rootLayoutManager.setHGap(1);
        rootLayoutManager.setVGap(1);
        rootLayoutManager.setMargin(new Insets(0, 0, 0, 0));
        rootPanel = new JBPanel<>();
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));

        initPanel();
        initText();


    }

    private void initText() {
    }

    private void initTopSplitPanel() {
        FlowLayout layout = new FlowLayout();
        layout.setVgap(1);
        layout.setHgap(1);
        layout.setAlignment(FlowLayout.LEFT);

        int topHeight = 30;
        Dimension dimension = new Dimension(-1, topHeight);
    }

    private void initTabbedPanel() {
        GridLayoutManager layoutManager = new GridLayoutManager(1, 1);
        layoutManager.setHGap(1);
        layoutManager.setVGap(1);
        layoutManager.setMargin(new Insets(0, 0, 0, 0));

        Dimension lineDimension = new Dimension(700, 35);
        GridLayoutManager inputLayoutManager = new GridLayoutManager(3, 1);
        inputLayoutManager.setHGap(1);
        inputLayoutManager.setVGap(1);
        inputLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager moduleLayoutManager = new GridLayoutManager(3, 1);
        moduleLayoutManager.setHGap(1);
        moduleLayoutManager.setVGap(1);
        moduleLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager lineLayoutManager = new GridLayoutManager(1, 2);
        lineLayoutManager.setHGap(1);
        lineLayoutManager.setVGap(1);
        lineLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        I18Config config = new I18Config();
        String locale = config.getLocale();
        for (int i = 0; i < ALLOW_LANGUAGE.length; i++) {
            JTextPane helpPanel = new JTextPane();
            helpPanel.setEditable(false);
            HTMLEditorKit editorKit = new HTMLEditorKit();
            helpPanel.setEditorKit(editorKit);
            helpPanel.setOpaque(true);

            StringBuffer buffer = new StringBuffer();

            buffer.append("<html><body><div>")

                    .append("<h2 id=\"allProject\"><a href=\"/java/codetag-swing.html\" title=\"热门实例\">热门实例</a></h2>       ")
                    .append("<ul>")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-basic.html\" title=\"basic\">basic</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-border.html\" title=\"border\">border</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-dialog.html\" title=\"dialog\">dialog</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-event.html\" title=\"event\">event</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jbutton.html\" title=\"jbutton\">jbutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jcheckbox.html\" title=\"jcheckbox\">jcheckbox</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jcombobox.html\" title=\"jcombobox\">jcombobox</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jdesktoppane.html\" title=\"jdesktoppane\">jdesktoppane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jdialog.html\" title=\"jdialog\">jdialog</a></li>  ")
                    .append("<li class=\"design action\"><a href=\"/java/codetag-swing-jeditorpane.html\" title=\"jeditorpane\">jeditorpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jfilechooser.html\" title=\"jfilechooser\">jfilechooser</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jformattedtextfield.html\" title=\"jformattedtextfield\">jformattedtextfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jframe.html\" title=\"jframe\">jframe</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jinternalframe.html\" title=\"jinternalframe\">jinternalframe</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlabel.html\" title=\"jlabel\">jlabel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlayer.html\" title=\"jlayer\">jlayer</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlist.html\" title=\"jlist\">jlist</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-joptionpane.html\" title=\"joptionpane\">joptionpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpanel.html\" title=\"jpanel\">jpanel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpasswordfield.html\" title=\"jpasswordfield\">jpasswordfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpopupmenu.html\" title=\"jpopupmenu\">jpopupmenu</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jprogressbar.html\" title=\"jprogressbar\">jprogressbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jradiobutton.html\" title=\"jradiobutton\">jradiobutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jscrollbar.html\" title=\"jscrollbar\">jscrollbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jscrollpane.html\" title=\"jscrollpane\">jscrollpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jslider.html\" title=\"jslider\">jslider</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jspinner.html\" title=\"jspinner\">jspinner</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jsplitpane.html\" title=\"jsplitpane\">jsplitpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtabbedpane.html\" title=\"jtabbedpane\">jtabbedpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtable.html\" title=\"jtable\">jtable</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextarea.html\" title=\"jtextarea\">jtextarea</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextfield.html\" title=\"jtextfield\">jtextfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextpane.html\" title=\"jtextpane\">jtextpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtogglebutton.html\" title=\"jtogglebutton\">jtogglebutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtoolbar.html\" title=\"jtoolbar\">jtoolbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtree.html\" title=\"jtree\">jtree</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jwindow.html\" title=\"jwindow\">jwindow</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-layout.html\" title=\"layout\">layout</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-look_and_feel.html\" title=\"look_and_feel\">look_and_feel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-swingworker.html\" title=\"swingworker\">swingworker</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-thread.html\" title=\"thread\">thread</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-timer.html\" title=\"timer\">timer</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-tooltip.html\" title=\"tooltip\">tooltip</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-uimanager.html\" title=\"uimanager\">uimanager</a></li>  ")
                    .append("</ul>")

                    .append("<h2 id=\"allProject\"><a href=\"/java/codetag-swing.html\" title=\"热门实例\">热门实例</a></h2>       ")
                    .append("<ul>")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-basic.html\" title=\"basic\">basic</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-border.html\" title=\"border\">border</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-dialog.html\" title=\"dialog\">dialog</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-event.html\" title=\"event\">event</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jbutton.html\" title=\"jbutton\">jbutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jcheckbox.html\" title=\"jcheckbox\">jcheckbox</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jcombobox.html\" title=\"jcombobox\">jcombobox</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jdesktoppane.html\" title=\"jdesktoppane\">jdesktoppane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jdialog.html\" title=\"jdialog\">jdialog</a></li>  ")
                    .append("<li class=\"design action\"><a href=\"/java/codetag-swing-jeditorpane.html\" title=\"jeditorpane\">jeditorpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jfilechooser.html\" title=\"jfilechooser\">jfilechooser</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jformattedtextfield.html\" title=\"jformattedtextfield\">jformattedtextfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jframe.html\" title=\"jframe\">jframe</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jinternalframe.html\" title=\"jinternalframe\">jinternalframe</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlabel.html\" title=\"jlabel\">jlabel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlayer.html\" title=\"jlayer\">jlayer</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jlist.html\" title=\"jlist\">jlist</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-joptionpane.html\" title=\"joptionpane\">joptionpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpanel.html\" title=\"jpanel\">jpanel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpasswordfield.html\" title=\"jpasswordfield\">jpasswordfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jpopupmenu.html\" title=\"jpopupmenu\">jpopupmenu</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jprogressbar.html\" title=\"jprogressbar\">jprogressbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jradiobutton.html\" title=\"jradiobutton\">jradiobutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jscrollbar.html\" title=\"jscrollbar\">jscrollbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jscrollpane.html\" title=\"jscrollpane\">jscrollpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jslider.html\" title=\"jslider\">jslider</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jspinner.html\" title=\"jspinner\">jspinner</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jsplitpane.html\" title=\"jsplitpane\">jsplitpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtabbedpane.html\" title=\"jtabbedpane\">jtabbedpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtable.html\" title=\"jtable\">jtable</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextarea.html\" title=\"jtextarea\">jtextarea</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextfield.html\" title=\"jtextfield\">jtextfield</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtextpane.html\" title=\"jtextpane\">jtextpane</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtogglebutton.html\" title=\"jtogglebutton\">jtogglebutton</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtoolbar.html\" title=\"jtoolbar\">jtoolbar</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jtree.html\" title=\"jtree\">jtree</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-jwindow.html\" title=\"jwindow\">jwindow</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-layout.html\" title=\"layout\">layout</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-look_and_feel.html\" title=\"look_and_feel\">look_and_feel</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-swingworker.html\" title=\"swingworker\">swingworker</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-thread.html\" title=\"thread\">thread</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-timer.html\" title=\"timer\">timer</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-tooltip.html\" title=\"tooltip\">tooltip</a></li>  ")
                    .append("<li class=\"design \"><a href=\"/java/codetag-swing-uimanager.html\" title=\"uimanager\">uimanager</a>" +
                            "<p><img src=\"/help/zh-images/分组方式.png\" title=\"接口分组1\" alt=\"接口分组1\" /></p>" +
                            "<p><img src=\"help/zh-images/分组方式.png\" title=\"接口分组2\" alt=\"接口分组2\" /></p>" +
                            "<p><img src=\"zh-images/分组方式.png\" title=\"接口分组4\" alt=\"接口分组4\" /></p>" +
                            "<p><img src=\"./help/zh-images/分组方式.png\" title=\"接口分组3\" alt=\"接口分组3\" /></p>" +
                            "</li>  ")
                    .append("</ul>")

                    .append("</div></body></html>");
            helpPanel.setText(buffer.toString());
            try {
//                File file3 = new File(HelpForm1.class.getClassLoader().getResource("javaapplication/file3.txt").getFile());
//                String file = HelpForm1.class.getResource("/help/" + ALLOW_LANGUAGE[i].getLanguage() + ".md").getFile();
                String content = FileUtils.readFileToString(new File(HelpForm1.class.getResource("/help/" + ALLOW_LANGUAGE[i].getLanguage() + ".md").getFile()), "utf-8");

                String htmlContent = MDTool.markdown2Html(content);
                helpPanel.setText(htmlContent);
                JScrollPane jbScrollPane = new JBScrollPane(helpPanel);
                jbScrollPane.setPreferredSize(new Dimension(-1, 500));
                helpTabbedPanel.addTab(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.switch_language.text.cur_" + ALLOW_LANGUAGE[i].getLanguage()), null, jbScrollPane);
                if (locale.equals(ALLOW_LANGUAGE[i].getLanguage())) {
                    helpTabbedPanel.setSelectedComponent(jbScrollPane);
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        Dimension dimension = new Dimension(-1, -1);

        rootPanel.add(helpTabbedPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
    }

    private void initButtonSplitPanel() {
    }

    private void initButtonPanel() {

    }

    private void initPanel() {
        rootPanel = new JPanel();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(1, 1);
        gridLayoutManager.setVGap(1);
        gridLayoutManager.setHGap(1);

        rootPanel.setLayout(gridLayoutManager);
        initTopSplitPanel();
        initTabbedPanel();
        this.add(rootPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        null, null, null));
    }

    public Project getProject() {
        return project;
    }


    private void initAction() {
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }

}
